<?php
$adm_menu = "1";

include("config.php");
if (!isset($_SESSION["auser"]) || $_SESSION["auser"] == "" || $_SESSION['agroup'] != "WEB_ADMIN") header('Location: index.php');

$db_www = new DB($server_username,$server_pass,$server_db,$server,$driver);
$db_rdata = new DB($rdata_server_username,$rdata_server_pass,$rdata_server_db,$rdata_server,$rdata_driver);

$err = "";


if (isset($_REQUEST["act"]) ){
		 if ($_REQUEST["act"] == "save") {		

			$arr = explode(';',$_REQUEST["lst"]);
			$sql = "create table #tt_eilutes(kodas varchar(100));";
			$db_rdata->execute($sql);	
			echo $_REQUEST["lst"];
			foreach($arr as $item)
			{
				if ($item != "") {
					$sql = "insert into #tt_eilutes VALUES('".addslashes_mssql($item)."');";
					$db_rdata->execute($sql);	
					
				}
			}
			

			$role_change_query = str_replace("<uid>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["uid"])), $role_change_query);
			$role_change_query = str_replace("<pav>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["pav"])), $role_change_query);
			$db_rdata->execute($role_change_query);
			header("Location: roles.php");
	}
}	
include("header.php");
include("menu.php");




	if (isset($_REQUEST["uid"]) && $_REQUEST["uid"] != "0") {
		$uid = @iconv('UTF-8','windows-1257',$_REQUEST["uid"]);

		if (!isset($_POST["uid"])){
			$sql = "select * from ws_roles_h where role_id= '".addslashes_mssql($uid)."'";
			$info = $db_rdata->returnInfo($sql);	
		

			
			if (!empty($info)) {
				$pav = @iconv('windows-1257', 'UTF-8', $info["role_pav"]);

			} else {
				$pav = "";

			}
			
		} else {
				$pav = $_POST["pav"];

				
		}
	} else {
		$uid = "0";

		
		if (!isset($_POST["uid"])){
			$pav = "";

		} else {
			$pav = $_POST["pav"];
	
		}
	}
	

	



?>
<script>
function validateForm(myform)
{
test ();
  pav = document.getElementById("pav");
	if (pav.value.trim() == "") {
		alert("Pavadinimas yra privalomas");
		return;
	}
	myform.submit();
}





</script>
<style>
#sortable1, #sortable2 { min-height:100px;list-style-type: none; margin: 0; padding: 0; float: left; margin-right: 10px; background: #eee; padding: 5px; width: 143px;}
#sortable1 li, #sortable2 li, { margin: 5px; padding: 5px; font-size: 1.2em; width: 120px; }
.bleft1 { padding:10px;border-left: 1px solid #cccccc;border-top: 1px solid #cccccc;border-bottom: 1px solid #cccccc;}
.bright1 { padding:10px;border-right: 1px solid #cccccc;border-top: 1px solid #cccccc;border-bottom: 1px solid #cccccc;}

</style>
<script>
$(function() {
$( "ul.droptrue" ).sortable({
connectWith: "ul"
});

$( "#sortable1, #sortable2" ).disableSelection();
});
function test (){
	sel =document.getElementById('sortable2');
	if (sel!=null){
	var items = sel.getElementsByTagName('li');
	lst =document.getElementById('lst');
	for (var i = 0; i < items.length; ++i) {
		lst.value = lst.value + ';'+ items[i].id.substring(1);
		
	}
	}
	
}
</script>
<form method="post" name='forma' action='role.php?act=save'>

<input id="lst" type="hidden" name="lst" value="">
<input id="uid" type="hidden" name="uid" value="<?php echo @iconv('windows-1257','UTF-8',$uid);?>">

<table class="innercont" cellspacing="0" cellpadding="10" border="0">
<tr><td class="leftcol2">
<input class='but bgwhite nopad' type = "submit" value = "Išsaugoti" onclick="validateForm('forma');return false;"><br><br>
<Button id='unblockButton' class='but bgwhite nopad' onclick="window.location='roles.php';return false;">Atgal</Button>
</td><td class="centercol">
<?php
if ($err != ""){
	echo "<p style='background-color:transparent;color:red;font-weight:bold;font-size:10px;'>".$err."</p>";
}
?>


<table width="100%" border="0">



<tr><td valign="top" colspan="2" class="fieldtitle">Rolės pavadinimas:</td><td valign="top" class="fieldtitle"></td></tr>
<tr>
<td valign="top" colspan="2">
<input style="width:275px;" id='pav' class="inp" type='text' name="pav" value="<?php echo htmlspecialchars($pav)?>">
</td>
<td valign="top">

</td></tr>
<?php

$sql ="select * from ws_roles_b where role_id='".addslashes_mssql($uid)."'";

$rgroups = $db_rdata->returnList($sql);	

$gr ='';
$ex ="''";
foreach ($rgroups as $item) {
	$gr .= '<li class="ui-state-default" id="v'.htmlspecialchars(trim($item["method_pav"])).'">'.@iconv('windows-1257', 'UTF-8',htmlspecialchars($item["method_pav"])).'</li>';
	$ex .= ",'".$item["method_pav"]."'";
}
$sql ="select * from ws_methods where method_pav not in(".$ex.")";
$groups = $db_rdata->returnList($sql);
?>
<tr><td valign="top"  class="fieldtitle">Galimi metodai:</td><td valign="top"  class="fieldtitle">Priskirti metodai</td></tr>
<tr>
<td valign="top" class='bleft1'>
<ul id="sortable1" class="droptrue">
<?php
foreach ($groups as $item) {
	echo '<li class="ui-state-default" id="v'.htmlspecialchars(trim($item["method_pav"])).'">'.@iconv('windows-1257', 'UTF-8',htmlspecialchars($item["method_pav"])).'</li>';

}
?>
</ul>
</td>
<td valign="top"  class='bright1'>


<ul id="sortable2" class="droptrue">
<?php
echo $gr;
?>
</ul>
</td></tr>

</table>


</td><td class="rightcol">
<?php

?>


</td></tr>
<tr><td>&nbsp;</td><td colspan="2">

</td></tr>
</table>
</form>
<?php
include("footer.php");
?>